package com.example.hand.controller;

import com.example.hand.bean.Admin;
import com.example.hand.bean.Order;
import com.example.hand.bean.PageBean;
import com.example.hand.bean.User;
import com.example.hand.dao.UserDao;
import com.example.hand.service.AdminService;
import com.example.hand.service.OrderService;
import com.example.hand.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;
    @Autowired
    UserDao userDao;

    @RequestMapping("login")//欢迎页面
    public String order() {
        return "adminLogin";
    }
    @RequestMapping("loginIn")
    public String login(String name, String password, HttpSession session,Map<String, Object> map){
        Admin admin;
        admin = adminService.login(name,password);
        if(admin!=null){
            session.setAttribute("name",name);
            session.setMaxInactiveInterval(-1);
            return "mainInfo";
        }else {
            return "adminLogin";
        }
    }

    @RequestMapping("adminDoUser")//主页查询
    public String main(Model model, @RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage){
        PageBean<User> pageBean = userService.userPage(currentPage);
        System.out.println(pageBean);
        model.addAttribute("pageBean",pageBean);
        return "adminDoUser";
    }

    @RequestMapping("userDelete")//删除
    public String delete(int user_id) {
        userDao.deleteByPrimaryKey(user_id);
        return "redirect:adminDoUser";//重定向到主页面
    }

    @RequestMapping("userEdit")//修改页
    public String userEdit(Model model, Integer user_id) {
        model.addAttribute("edit", userDao.selecttest(user_id));
        System.out.print(66);
        return "userEdit";
    }
    @RequestMapping("userUpdate")//修改执行
    public String userupdate(String user_id, String user_name, String user_password, String user_age, String user_sex ,
                             HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {
        User user   = new User();

        user.setUser_name(user_name);
        user.setUser_password(user_password);
        user.setUser_age(user_age);
        user.setUser_sex(user_sex);
        user.setUser_id(new Integer(user_id));
        int n = userDao.updateByPrimaryKeySelective(user);//调用更新方法对数据库进行更行（执行更新的sql语句）
        List<User> userList = userDao.selectByAll();//数据库更新之后把数据查询出来
        model.addAttribute("userlist", userList);
        return "redirect:adminDoUser";//重定向到userInfo页面

    }
    //模糊查询，搜索框
    @RequestMapping(value = "selectUserByLike")
    public String selectUserByLike(HttpServletRequest request, HttpServletResponse response,
                                    HttpSession session, Model model, User user,String content){
        if(content==""){
            return "redirect:adminDoUser";
        }
        List<User> userList = userDao.queryUserByLike(content);
        PageBean<User>  pageBean = new PageBean<>();
        pageBean.setLists(userList);
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("user",userList);//这里注意order要和html中的查询遍历那里的名字一样，否则查询框里没有数据。
        return "adminDoUser";


    }

    @RequestMapping("admin_logout")
    public String logout( HttpSession session){
        session.invalidate();
        return "adminLogin";
    }
    @RequestMapping("aaa")
    public String aaa(){
        return "mainInfo";
    }
}
